﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SmartSpace.WebApp
{
    public class SspModule : IHttpModule
    {
        private HttpApplication _context;

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            _context = context;

            context.BeginRequest += context_BeginRequest;
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            string path = _context.Request.Url.AbsolutePath.ToLower();
            if (path != "/" && !path.Contains('.'))
            {
                // 处理 http://host/name 这样的地址
                string page = path.Substring(1);

                context.RewritePath(string.Format("./_shell.aspx?ss:page={0}&{1}", page, context.Request.QueryString));
            }
            else if (path.EndsWith(".ssvp"))
            {
                // 处理 http://host/name.ssvp 这样的地址
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("(?<page>[^\\/]*).ssvp");
                System.Text.RegularExpressions.Match match = regex.Match(path);
                string page = match.Groups["page"].Value;

                //context.RewritePath(string.Format("./_shell.aspx?ss:page={0}&{1}", page, context.Request.QueryString));
            }
        }


    }
}